package core.utils;

/**
 * Wrapper-class for the name and unit of a variable.
 */
public final class Variable {
	private final String name, unit;
    private int cachedHashCode = 0;
    
	public Variable(String name, String unit) {
		this.name = name;
		this.unit = unit;
	}

	/**
	 * Returns the name of the variable.
	 * 
	 * @return The name of the variable.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Returns the unit of the variable.
	 * 
	 * @return The unit of the variable.
	 */
	public String getUnit() {
		return unit;
	}

    /**
     * A variable is equal to another if their names and units are equal.
     * @param o Object to compare.
     * @return True if this object is equal to o, false otherwise.
     */
	@Override
	public boolean equals(Object o) {
		if (!(o instanceof Variable)) {
			return false;
		}
		Variable var = (Variable) o;
		return (var.name.equals(name) && var.unit.equals(unit));
	}

    @Override
    public int hashCode() {
        if (cachedHashCode == 0){
            String s = name + unit;
            cachedHashCode = s.hashCode();
        }
        return cachedHashCode;
    }

    @Override
    public String toString(){
        return "Variablename: " + name + " Unit: " + unit;
    }
}
